package Practice;

import java.util.LinkedList;
import java.util.Queue;

/**
 * create by Sun
 * 2018/8/16
 */
 class Tree{
    public char val;
     public Tree cl;
    public Tree rl;

}
public class CreateTree {
     private static char[] a="abc,,de,g,,f,,,".toCharArray();
     private static Integer flag=-1;

     public static void main(String[] args) {
         Tree tree=null;
         tree=createTree(tree);
         //xianxu(tree);
         cengci(tree);
         System.out.println(deep(tree));
    }

    private static int deep(Tree tree) {
         if(tree==null) return 0;
         else{
             int deepcl=deep(tree.cl);
             int deeprl=deep(tree.rl);
             return deepcl>deeprl?deepcl+1:deeprl+1;
         }
    }

    static void cengci(Tree tree){
         Queue<Tree> queue=new LinkedList<>();
         Tree concrentTree=null;
         queue.add(tree);
         while (queue.isEmpty()!=true){
             concrentTree=queue.poll();
             System.out.println(concrentTree.val);
             if(concrentTree.cl!=null){
                 queue.add(concrentTree.cl);
             }
             if(concrentTree.rl!=null){
                 queue.add(concrentTree.rl);
             }
         }
    }


    /**
     *
     * @param tree
     */
    private static void xianxu(Tree tree) {
         if(tree!=null){
             System.out.print(tree.val+" ");
             xianxu(tree.cl);
             xianxu(tree.rl);
         }
    }

    /**
     * 建树
     * @param tree
     * @return
     */
    private static Tree createTree(Tree tree) {
         char x=a[++flag];
         if (x==',') return null;
         else{
             tree=new Tree();
             tree.val=x;
             tree.cl=createTree(tree.cl);
             tree.rl=createTree(tree.rl);
            return tree;
         }
    }

}
